/**
 * Created by chenwenhao on 2017/3/3.
 * @description
 * 此模块包含用户登录/用户注册HTTP模块
 */

import {Injectable} from "@angular/core";
import {ToolsService} from '../tools/tools.service';
import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";
// import {AuthCodeRespVO, AuthorizeVO, OpenAuthVO} from "@interface/feedback.model";
import {Observable} from "rxjs";
import {Response} from "@angular/http";
import {AuthCodeRespVO, AuthorizeVO, SNSResult, UserInfoVO} from "@interface/index";



@Injectable()
export class UserActivityService {
  public LOGIN_URL = '/user/_guest/login';
  public REGISTER_URL = '/user/_guest/register';
  public GET_SMS_CODE_URL = '/user/_guest/smsCode';
  public VALIDATE_SMS_CODE_URL = '/user/_guest/validateSmsCode';
  public NICKNAME_URL = '/user/_userInfo';
  public IMG_VALIDATE_CODE_URL = '/guest/generateImgValidateCode'; // 获取图片验证码 @author ouyqx-691
  public CHECK_IMG_VALIDATE_CODE_URL = '/guest/checkImgValidateCode'; // 验证图片验证码 @author ouyqx-691

  constructor(public httpService: HttpService,
              public tools: ToolsService) {
  }

  login(data) {
    const url = this.LOGIN_URL;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }

   // 获取短信验证码
  getSMSCode(data) {
    const url = this.GET_SMS_CODE_URL;
    const uri = this.tools.param(data);
    return this.httpService.get(`${url}?${uri}`).map((res) => res.json());
  }

   // 核实短信验证码
  checkSMSCode(data) {
    const url = this.VALIDATE_SMS_CODE_URL;
    const uri = this.tools.param(data);
    return this.httpService.get(`${url}?${uri}`).map((res) => res.json());
  }

   // 注册
  register(data) {
    const url = this.REGISTER_URL;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }

   // 设置昵称
  setNickname(data) {
    const url = this.NICKNAME_URL;
     //  return this.httpService.userPut(`${url}?${data}`).map((res) => res.json());
    return this.httpService.putUrlencode(url, data).map((res) => res.json());
  }

  /**
   * @description 获取图片验证码
   * @author ouyqx-691
   * @date 2017-09-19
   * @param params{type:类型,width:宽度,height:高度}
   */
  getImgValidateCode(params) {
    const url = this.IMG_VALIDATE_CODE_URL + '?' + this.tools.param(params);
    return this.httpService.get(`${url}`).map((res) => res.json());
  }

  /**
   * @description 验证图片验证码
   * @author ouyqx-691
   * @date 2017-09-19
   * @param params{randomKey:关键字,imgValidateCode:图片验证码,type:类型}
   */
  checkImgValidateCode(params) {
    const url = this.CHECK_IMG_VALIDATE_CODE_URL + '?' + this.tools.param(params);
    return this.httpService.get(`${url}`).map((res) => res.json());
  }

  /**
   * @description 发起第三方登陆
   * type == 1 => qq登陆
   * type == 2 => 微信公众号登陆（作废用下面的doWechatLogin）
   * type == 3 => 微博登录
   * @author @chenwh576
   * @date 2017-11-21
   * @param {number} type
   * @return {Observable<SNSResult<AuthCodeRespVO>>}
   */
  callThirdLogin(type: number, callBackUrl: string): Observable<SNSResult<AuthCodeRespVO>> {
    const url = API.USER_OPENID_LOGIN + type + '&callBackUrl=' + callBackUrl;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 微信登陆
   * @author @chenwh576
   * @date 2017-11-21
   * @returns {Observable<any>}
   */
  callWechatLogin(): Observable<SNSResult<AuthorizeVO>> {
    const url = API.AUTH_JUMP_AUTH + '?type=h5.login';
    return this.httpService.get(url, true)
    .map((res: Response) =>  res.json());

  }

  /**
   * @description 第三方登陆
   * 发起第三方登陆后在跳转页面发起此请求与后台做登陆
   * @author @chenwh576
   * @date 2017-11-21
   * @param urlParams
   * @return {Observable<SNSResult<UserInfoVO>>}
   */
  ThirdLogin(urlParams: string, thirdType: string): Observable<SNSResult<UserInfoVO>> {
    const url = `${API.THIRD_LOGIN[thirdType]}${urlParams}`,
      reqType = thirdType === 'wechat' ? true : false;
    return this.httpService.get(url, reqType).map((res: Response) => res.json());
  }


}
